import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_module_sale/common/config/page_base_common.dart';
import 'package:dk_flutter_module_sale/common/user_track/user_track.dart';
import 'package:dk_flutter_module_sale/config/route/sale_in_widget_route.dart';
import 'package:dk_flutter_module_sale/sale/api/sale_in_api.dart';
import 'package:dk_flutter_module_sale/sale/sale_in/contract_change/bean/bean_contract_change.dart';

@ARoute(url: SaleInWidgetRoute.SALE_IN_CONTRACT_CHANGE)
class ContractChange extends CommonPageWithTrack{
  ContractChange(BoostRouteOption option) : super();

  @override
  void trackStatistics() {
    UserTrack.trackViewScreen("收房-收房工单-列表", null);
  }

  @override
  DkAppBar getAppBar(CommonPageBloc pageBloc) {
    return DkAppBar(
        DKAppBarParams.withBackNoMenu(pageBloc.context, titleContent: "收房工单"));
  }

  @override
  void onInitState(CommonPageBloc pageBloc) {
    pageBloc.pageHttpGet(
        path: SaleInApi.contractChangeList,
        widgetBuilderByData: (dynamic data) {
          return getContentWidget(BeanContractChangeList.fromJson(data).list);
        });
  }

  List<Widget> buildGridTileList(List<BeanContractChange> list) {
    List<Widget> widgetList = new List();
    for (int i = 0; i < list.length; i++) {
      widgetList.add(getItemWidget(list[i]));
    }
    return widgetList;
  }

  Widget getItemWidget(BeanContractChange bean) {
    //BoxFit 可设置展示图片时 的填充方式
    return InkWell(
      onTap: () => RouteUtil.goPage(bean?.url),
      child: Stack(
        fit: StackFit.expand,
        alignment: FractionalOffset(0.5, 0.5),
        children: <Widget>[
          CachedNetworkImage(
            imageUrl: bean?.backgroundUrl,
            fit: BoxFit.fill,
          ),
          Positioned(
            child: CachedNetworkImage(imageUrl: bean?.iconUrl),
            width: 41,
            height: 41,
          ),
          Positioned(
            child: Text(bean?.title,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(color: Colors.white, fontSize: 16)),
            bottom: 24,
          )
        ],
      ),
    );
  }

  Widget getContentWidget(List<BeanContractChange> list) {
    return listNotEmpty(list)
        ? GridView.count(
            shrinkWrap: true,
            primary: false,
            padding: EdgeInsets.fromLTRB(14, 23, 14, 0),
            //横向Item的个数
            crossAxisCount: 2,
            children: buildGridTileList(list))
        : LogicPage.emptyPage();
  }

}
